package com.by.model;

import com.by.constant.BusinessEnum;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 * @author ServivE
 * @version 0.1
 * @className Result
 * @description
 * @date 2024/12/4 10:37
 * @since jdk17
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
@ApiModel("项目统一响应结果对象")
public class Result<T> implements Serializable {

    /**
     * 响应码
     */
    @ApiModelProperty("状态码")
    private Integer CODE = 200;

    /**
     * 响应信息
     */
    @ApiModelProperty("消息")
    private String MSG = "ok";

    /**
     * 响应数据
     */
    @ApiModelProperty("消息")
    private T DATA;

    /**
     * 操作成功
     * @param data
     * @return
     * @param <T>
     */
    public static <T> Result<T> success(T data) {
        Result<T> result = new Result<>();
        result.setDATA(data);
        return result;
    }

    /**
     * 操作失败
     * @param businessEnum
     * @return
     * @param <T>
     */
    public static <T> Result<T> fail(BusinessEnum businessEnum) {
        Result<T> result = new Result<>();
        result.setCODE(businessEnum.getCode());
        result.setMSG(businessEnum.getDesc());
        return result;
    }

    /**
     * 操作失败
     * @param code
     * @param msg
     * @return
     * @param <T>
     */
    public static <T> Result<T> fail(Integer code, String msg) {
        Result<T> result = new Result<>();
        result.setCODE(code);
        result.setMSG(msg);
        return result;
    }
    /**
     * 业务处理结果
     * @param flag
     * @return
     */
    public static Result<String> handle(Boolean flag) {
        // 判断是否处理成功
        if (flag) {
            // 业务处理成功
            return success(null);
        }
        // 业务处理失败，返回失败信息
        return fail(BusinessEnum.OPERATION_FAIL);
    }
}
